package com.tian.service.impl.manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tian.mapper.ItemManageMapperCustom;
import com.tian.mapper.UserManageMapperCustom;
import com.tian.pojo.Users;
import com.tian.pojo.bo.ItemManageBO;
import com.tian.pojo.bo.UserManageBO;
import com.tian.pojo.vo.ItemManageVO;
import com.tian.pojo.vo.UserManageVO;
import com.tian.service.UserService;
import com.tian.service.manage.ItemManageService;
import com.tian.service.manage.UserManageService;
import com.tian.utils.DateUtil;
import com.tian.utils.MD5Utils;
import com.tian.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemManageServiceImpl implements ItemManageService {

    @Autowired
    private ItemManageMapperCustom itemManageMapperCustom;



    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryPagedItemManage(Integer page, Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page,pageSize);
        List<ItemManageVO> list = itemManageMapperCustom.queryPagedItemManage();

        return setterPagedGrid(list,page);
    }

    @Override
    public List<ItemManageVO> getGoodManageByGoodName(String goodname) {
        List<ItemManageVO> list = itemManageMapperCustom.getGoodManageByGoodName(goodname);
        return list;
    }

    @Override
    public int updateItemManage(ItemManageBO itemManageBO) {
        Map<String,Object> map=new HashMap<>();
        map.put("stock",itemManageBO.getStock());
        map.put("priceNormal",itemManageBO.getPriceNormal());
        map.put("priceDiscount",itemManageBO.getPriceDiscount());
        map.put("itemname",itemManageBO.getItemname());
        map.put("specname",itemManageBO.getSpecname());
        int i = itemManageMapperCustom.updateItemManage(map);
        return i;
    }


    private PagedGridResult setterPagedGrid(List<?> list,Integer page){
        PageInfo<?> pageList =new PageInfo<>(list);
        PagedGridResult grid=new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
